let rewardedVideoAd = null
const {
    javaUrl,
    baseUrl,
    xxtUrl
} = require("../../utils/util")

// pages/activity/activity.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        phone: '',
        password: '',
        active_id: '',
        user: {},
        course_info: {},
        show: 0,
        choice: [],
        getActiveIdloading: false,
        getActiveIdDisable: false,
        showConfirm: false,
        getActiveText: '获取正在签到的活动',
        current: 0, //当前所在页面的 index

        indicatorDots: true, //是否显示面板指示点

        autoplay: true, //是否自动切换

        interval: 3000, //自动切换时间间隔

        duration: 800, //滑动动画时长
        circular: true, //是否采用衔接滑动
        imgUrls: [],
        links: [

            // '/pages/index/index',

            // '/pages/index/index',

            // '/pages/index/index',

        ]

    },
    showDialog(e) {
        if (this.data.password == '') {
            wx.showToast({
                title: '请登陆后使用',
            })
            setTimeout(function () {
                wx.navigateTo({
                    url: '/pages/xuexitong/my/my',
                })
            }, 500)
        }
        wx.navigateTo({
            url: '/pages/xuexitong/presign/presign',
        })
    },

    closeDialog() {
        const {
            dialogKey
        } = this.data;
        this.setData({
            [dialogKey]: false
        });
    },
    presign() {

        rewardedVideoAd.show()
            .catch(() => {
                rewardedVideoAd.load()
                    .then(() => rewardedVideoAd.show())
                    .catch(err => {
                        wx.showToast({
                            title: '激励视频 广告显示失败',
                            icon: 'error'
                        })
                        console.log('激励视频 广告显示失败')
                    })
            })

        const {
            dialogKey
        } = this.data;
        this.setData({
            [dialogKey]: false
        });
    },
    goto_presign_old(event) {
        console.log("event", event);
        wx.navigateTo({
            url: '../xuexitong/old_presign/presign',
        })
    },


    //轮播图的切换事件

    swiperChange: function (e) {
        // console.log("轮播图切换",e)
        this.setData({
            swiperCurrent: e.detail.current
        })

    },

    //点击指示点切换
    chuangEvent: function (e) {
        console.log("轮播图切换", e)
        this.setData({

            swiperCurrent: e.currentTarget.id

        })


    },

    //点击图片触发事件

    swipclick: function (e) {
        console.log("轮播图切换点击图片触发事件", e)
        console.log(this.data.swiperCurrent);

        wx.navigateToMiniProgram({
            appId: 'wxdb15fcbc4f9a0706',
        })

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        this.getchoice()
        if (wx.createRewardedVideoAd) {
            rewardedVideoAd = wx.createRewardedVideoAd({
                adUnitId: 'adunit-70bc4b8ff22be701'
            })
            rewardedVideoAd.onLoad(() => {
                console.log('onLoad event emit')
            })
            rewardedVideoAd.onError((err) => {
                console.log('onError event emit', err)
            })
            rewardedVideoAd.onClose((res) => {
                if (res.isEnded) {
                    wx.navigateTo({
                        url: '/pages/xuexitong/presign/presign',
                    })
                } else {
                    wx.showToast({
                        title: '未看完广告',
                    })
                }
                console.log('onClose event emit', res)
            })
        }




    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    getActiveIdBut() {
        if (this.data.password == '') {
            wx.showToast({
                title: '请登陆后使用',
                duration: 1500,
                success() {
                    wx.navigateTo({
                        url: '/pages/xuexitong/my/my',
                    })
                }
            })
        }
        wx.showLoading({
            title: '加载中...',
            duration: 1500
        })
        var that = this;
        that.setData({
            getActiveIdloading: true,
            getActiveIdDisable: true
        })


        wx.request({
            url: xxtUrl + '/activity',
            method: 'POST',
            data: {
                uf: that.data.user.uf,
                _d: that.data.user._d,
                vc3: that.data.user.vc3,
                uid: that.data.user.UID,
            },
            success(res) {

                console.log("res", res)
                if (res.data == 'NoActivity') {
                    that.countDown(that, 5)
                    wx.showToast({
                        title: '没有要签到课程',
                        icon: 'error',
                        duration: 3000

                    })
                    that.setData({
                        show: 0
                    })
                    return
                }
                wx.hideLoading({
                    success: (res) => {},
                })
                console.log("----" + res)

                that.setData({
                    course_info: res.data,
                    show: 1,
                })

                that.countDown(that, 5)
            },
            error(e) {
                console.log(e)
            }
        })
    },

    //倒计时60秒
    countDown(that, count) {
        if (count == 0) {
            that.setData({
                getActiveText: "获取正在签到的活动",
                getActiveIdloading: false,
                getActiveIdDisable: false
            })
            return;
        }
        that.setData({
            counting: true,
            getActiveText: count + '秒后重新获取',
        })
        setTimeout(function () {
            count--;
            that.countDown(that, count);
        }, 1000);
    },


    signout: function () {
        wx.removeStorage({
            key: 'phone',

        })
        wx.removeStorage({
            key: 'pwd',
        })
        wx.navigateTo({
            url: '/pages/xuexitong/my/my',
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this
        // console.log("option",options.code)
        console.log("local", wx.getStorageSync('login'))

        that.setData({
            phone: wx.getStorageSync('phone'),
            password: wx.getStorageSync('pwd'),

        })
        wx.request({

            url: xxtUrl + '/login',
            method: 'POST',
            data: {
                phone: this.data.phone,
                password: this.data.password
            },
            success(res) {
                console.log("denglu", res)
                wx.setStorageSync('signup', res.data)
                that.setData({
                    user: res.data,

                })

            }

        })

        wx.request({
            url: javaUrl + '/xxt/index-show/gettext',
            method: 'GET',
            success(res) {
                console.log("res", res)
                that.setData({
                    text: res.data.data.show.indexText
                })
            }
        })
    },

    getchoice() {
        var that = this
        wx.request({
            url: javaUrl + '/xxtautosignjava/index-choice/getchoice',
            method: 'GET',
            success(res) {
                console.log('getchoice', res)
                that.setData({
                    choice: res.data.data.choice
                })
                console.log('data ', that.data.choice)
            }

        })
        wx.request({
            url: javaUrl + '/xxt/index-show-img/getImgList',
            method: 'GET',
            success(res) {
                console.log('getImgList', res)
                that.setData({
                    imgUrls: res.data.data.imgList,
                    // links:res.data.data.links
                })
            }
        })

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {


    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})